import 'package:fluent_ui/fluent_ui.dart';

import '../../api/school_grade_api.dart';
import '../../api/school_semester_api.dart';
import '../../model/school_grade_model.dart';
import '../../model/school_semester_model.dart';
import '../../model/semester_model.dart';

///应用学校数据
class AppSchoolData {
  AppSchoolData._();

  static final _singleton = AppSchoolData._();

  factory AppSchoolData() => _singleton;

  final _schoolSemestersMap = <String, Map<String, SemesterItemRes>>{};
  final _schoolGradesMap = <String, Map<String, SchoolGradeRes>>{};

  ///加载学校学期数据
  Future<List<SemesterItemRes>> semestersBySchool(final String schoolId) async {
    if (schoolId.isNotEmpty) {
      final map = _schoolSemestersMap[schoolId];
      if (map == null || map.isEmpty) {
        return schoolSemesterApi
            .query(
          SchoolSemesterQueryReq(
            schoolId: schoolId,
            status: 1,
            index: 0,
            size: 100,
          ),
        )
            .then(
          (res) {
            final map_ = <String, SemesterItemRes>{};
            if (res.isSuccessed && res.rows.isNotEmpty) {
              for (var row in res.rows) {
                map_[row.id] = row;
              }
              _schoolSemestersMap[schoolId] = map_;
            }
            return map_.values.toList();
          },
        );
      } else {
        return Future<List<SemesterItemRes>>.value(map.values.toList());
      }
    }
    return Future<List<SemesterItemRes>>.value([]);
  }

  ///加载学校年级数据
  Future<List<SchoolGradeRes>> gradesBySchool(final String schoolId) async {
    if (schoolId.isNotEmpty) {
      final map = _schoolGradesMap[schoolId];
      if (map == null || map.isEmpty) {
        return schoolGradeApi
            .grades(SchoolGradeReq(schoolId: schoolId))
            .then((items) {
          final map_ = <String, SchoolGradeRes>{};
          if (items.isNotEmpty) {
            for (var item in items) {
              map_[item.val] = item;
            }
            _schoolGradesMap[schoolId] = map_;
          }
          return map_.values.toList();
        });
      } else {
        return Future<List<SchoolGradeRes>>.value(map.values.toList());
      }
    }
    return Future<List<SchoolGradeRes>>.value([]);
  }

  ///构建学期选项集合
  Future<List<ComboBoxItem<String>>> buildSemesterOpts(final String schoolId) =>
      semestersBySchool(schoolId).then((items) {
        final opts = <ComboBoxItem<String>>[];
        if (items.isNotEmpty) {
          for (var item in items) {
            opts.add(ComboBoxItem<String>(
              value: item.id,
              child: Text(item.title),
            ));
          }
        }
        return opts;
      });

  ///构建年级选项集合
  Future<List<ComboBoxItem<String>>> buildGradeOpts(final String schoolId) =>
      gradesBySchool(schoolId).then((items) {
        final opts = <ComboBoxItem<String>>[];
        if (items.isNotEmpty) {
          for (var item in items) {
            opts.add(ComboBoxItem<String>(
              value: item.val,
              child: Text(item.label),
            ));
          }
        }
        return opts;
      });

  ///获取学期标签名称集合
  String getSemesterLabels(
      {required String schoolId, List<String>? semesterIds}) {
    if (schoolId.isNotEmpty && semesterIds != null && semesterIds.isNotEmpty) {
      final map = _schoolSemestersMap[schoolId];
      if (map != null && map.isNotEmpty) {
        final labels = <String>[];
        for (var semesterId in semesterIds) {
          if (semesterId.isEmpty) {
            continue;
          }
          final data = map[semesterId];
          if (data != null) {
            labels.add(data.title);
          }
        }
        return labels.join(',');
      }
    }
    return '';
  }

  ///获取年级标签名称集合
  String getGradeLabels<T>({required String schoolId, List<T?>? gradeVals}) {
    if (schoolId.isNotEmpty && gradeVals != null && gradeVals.isNotEmpty) {
      final map = _schoolGradesMap[schoolId];
      if (map != null && map.isNotEmpty) {
        final labels = <String>[];
        for (final gradeVal in gradeVals) {
          if (gradeVal == null) {
            continue;
          }
          final data = map['$gradeVal'];
          if (data != null) {
            labels.add(data.label);
          }
        }
        return labels.join(',');
      }
    }
    return '';
  }

  ///获取学校学期名称
  String getSemesterLabel({required final String schoolId, semesterId}) =>
      getSemesterLabels(schoolId: schoolId, semesterIds: [semesterId]);

  ///获取学校年级名称
  String getGradeLabel<T>({required final String schoolId, T? gradeVal}) =>
      getGradeLabels<T>(schoolId: schoolId, gradeVals: [gradeVal]);
}

//应用学校数据单例
final appSchoolData = AppSchoolData();
